import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

function requireAll(requireContext) {
  return requireContext.keys().map(requireContext)
}

function lowerCaseFirstWord(word) {
  return word.charAt(0).toLowerCase() + word.slice(1)
}

/**
 * 导出所有views文件中的文件
 * @return {[array]} route配置数组
 */
function createRoutesByViews() {
  const modules = requireAll(require.context('../views', true, /^\.\/.*\.(vue|js)$/))
  return modules.map(module => ({
    path: `/${lowerCaseFirstWord(module.name)}`,
    name: module.name,
    component: module,
  }))
}

/**
 * 自定义routes规则
 * @return {[array]}
 */
function customRoutes() {
  return []
}

export default new Router({
  routes: createRoutesByViews().concat(customRoutes()),
})
